//app.js
App({
  onLaunch: function() {
    // 展示本地存储能力
    var logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs);
    const that = this;
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          wx.request({
            url: that.globalData.url + '/wechat/getOpenId',
            data: 'code=' + res.code,
            method: 'POST',
            header: {
              'content-type': 'application/x-www-form-urlencoded',
            },
            success: function(response) {
              if (response.data.success) {
                that.globalData.openId = response.data.content;
                //检查学员是否注册
                that.check();
              } else {
                wx.showToast({
                  title: '获取OPENID失败,请重新进入小程序',
                  icon: 'none'
                })
              }
              // 获取用户信息
              wx.getSetting({
                success: res => {
                  if (res.authSetting['scope.userInfo']) {
                    // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
                    wx.getUserInfo({
                      success: res => {
                        // 可以将 res 发送给后台解码出 unionId
                        that.globalData.userInfo = res.userInfo
                        //检查学员是否注册
                        that.check();
                        // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
                        // 所以此处加入 callback 以防止这种情况
                        if (that.userInfoReadyCallback) {
                          that.userInfoReadyCallback(res)
                        }
                      }
                    })
                  }
                }
              })
            }
          })
        }
      }
    })
  },
  globalData: {
    companyName:'先达健身',
    userInfo: null,
    openId: '',
    memberLogin: false,
    url: 'https://health.szcinda.cn/api',baseUrl: 'https://health.szcinda.cn',
    // url: 'http://localhost:8080/api',baseUrl:'https://health.szcinda.cn',
    emsItems: [{
      position: '二头',
      columnName: 'ertou',
      checked: true
    }, {
      position: '背阔',
      columnName: 'beikuo',
      checked: true
    }, {
      position: '三头',
      columnName: 'santou',
      checked: true
    }, {
      position: '下背',
      columnName: 'xiabei',
      checked: true
    }, {
      position: '胸部',
      columnName: 'xiongbu',
      checked: true
    }, {
      position: '臀部',
      columnName: 'tunbu',
      checked: true
    }, {
      position: '腹部',
      columnName: 'fubu',
      checked: true
    }, {
      position: '前腿',
      columnName: 'qiantui',
      checked: true
    }, {
      position: '上背',
      columnName: 'shangbei',
      checked: true
    }, {
      position: '后腿',
      columnName: 'houtui',
      checked: true
    }],
    bodyMeasurements: [{
        name: '胸围',
        value: 0,
        columnName: 'xiongwei'
      },
      {
        name: '腰围',
        value: 0,
        columnName: 'yaowei'
      },
      {
        name: '臀围',
        value: 0,
        columnName: 'tunwei'
      },
      {
        name: '左大腿',
        value: 0,
        columnName: 'zuodatui'
      },
      {
        name: '右大腿',
        value: 0,
        columnName: 'youdatui'
      },
      {
        name: '左臂',
        value: 0,
        columnName: 'zuobi'
      },
      {
        name: '右臂',
        value: 0,
        columnName: 'youbi'
      },
      {
        name: '肩宽',
        value: 0,
        columnName: 'jiankuan'
      }
    ],
  },
  check: function() {
    const that = this;
    const openId = that.globalData.openId;
    if (openId.length == 0) {
      return;
    }
    //如果此时userInfo还是null,则再获取一次用户数据
    if (that.globalData.userInfo == null) {
      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
            wx.getUserInfo({
              success: res => {
                // 可以将 res 发送给后台解码出 unionId
                that.globalData.userInfo = res.userInfo;
                this.checkHasInfo(openId, res.userInfo.nickName, res.userInfo.avatarUrl);
              }
            })
          }
        }
      })
    } else {
      const nickName = that.globalData.userInfo.nickName;
      const avatarUrl = that.globalData.userInfo.avatarUrl;
      this.checkHasInfo(openId, nickName, avatarUrl);
    }

  },
  checkHasInfo: function(openId, nickName, avatarUrl) {
    const url = this.globalData.url;
    wx.request({
      url: url + '/wechat/check',
      data: {
        openId: openId,
        nickName: nickName,
        avatarUrl: avatarUrl
      },
      method: 'POST',
      success: function(response) {}
    });
  }
})